<?php

/**
 *
 * Copyright (c) 2011 Tomáš Holub (tomas.holub@webadvice.cz)
 * 
 * @author Tomáš Holub (tomas.holub@webadvice.cz)
 * @package OverFishCMS
 */

namespace OverFish\Model\Mappers;

use Nette;
use OverFish;
use OverFish\Model\Entities\MenuItem as MenuItemEntity;

class MenuItem extends Nette\Object implements OverFish\Model\IMapper {

    private $conn;

    public function __construct(\DibiConnection $conn) {
        $this->conn = $conn;
    }
    
    public function delete($item) {
        $this->conn->delete('menu_item')
            ->where('id = %i', $item->getId())->execute();
    }
    
    public function save($item) {

        if ($item->getId() === NULL) { // insert
            $data = $this->itemToData($item); // vytáhne data z entity a vrátí jako pole
            
            $id = $this->conn->insert('menu_item', $data)->execute();
            $id = $this->conn->insertId();

            $this->setIdentity($item, $id);

        } else { // update
            $data = $this->itemToData($item); 
            
            $this->conn->update('menu_item', $data)
                ->where('id = %i', $item->getId())->execute();

        }
    }
    
    
    public function itemToData($item) {
        $data = array();
        
        $data = array(
            "title" => $item->getTitle(),
            "id_menu" => $item->getId_menu(),
            "slug" => $this->getSlug($item),
            "id_menu_item" => $item->getId_menu_item(),
            "id_page" => $item->getId_page(),
            "priority" => $item->getPriority(),
        );
        
        return $data;
    }
    
    protected function getSlug($item) {
        $result = array(\Nette\Utils\Strings::webalize($item->getTitle()));
        
        while (True) {
            $data = $this->getParent($item);

            if ($data) {
                $item = $this->load($data);
                
                $result[] = \Nette\Utils\Strings::webalize($item->getTitle());
                
            } else {
                break;
            }
        }
        
        
        return implode("/", array_reverse($result));
    }
    
    protected function getParent($item) {
        $items = $this->findBy(array("id", $item->getId_menu_item()), $raw = True);
        return array_shift($items);
        
    }
    
    public function find($id) {
        $data = $this->conn->select('*')
                        ->from('menu_item')
                        ->where('id = %i', $id)->fetch();

        return $this->load($data);
    }

    public function load($data) {
        $item = new MenuItemEntity();

        if (!$data) {
            return False;
        }

        $this->setIdentity($item, $data->id);


        $item->setTitle($data->title);
        $item->setId_menu($data->id_menu);
        $item->setSlug($data->slug);
        $item->setId_menu_item($data->id_menu_item);
        $item->setId_page($data->id_page);
        $item->setPriority($data->priority);
        
        if ($data->id_page) {
            $result = $this->conn->select("type, title")
                    ->from("page")
                    ->where("id = %i", $data->id_page)->fetch();
            $item->setPage_title($result->title);
            $item->setPage_type($result->type);
        }
        

        return $item;
    }
    
    public function findByParentAndMenu($menuId, $parentId) {
        $items = $this->conn->select('*')
                    ->from('menu_item');
        
        if ($parentId == Null) {
            $items->where("id_menu_item Is Null");
        } else {
            $items->where("id_menu_item = %s", $parentId);
        }
        
        $items = $items->and("id_menu = %s", $menuId)->orderBy("priority")->fetchAll();
        
        $objects = array();
        
        foreach ($items as $item) {
            $objects[] = $this->load($item);
        }
        
        return $objects;
    }
            
    
    function findBy(array $values, $raw = False) {
        $items = $this->conn->select('*')
                    ->from('menu_item');
        
        
        
        if ($values[1] == Null) {
            $items->where($values[0] . " Is Null");
        } else {
            $items->where($values[0] . " = %s", $values[1]);
        }
        
        $items = $items->fetchAll();
        
        if ($raw) {
            return $items;
        }
        
        $objects = array();
        
        foreach ($items as $item) {
            $objects[] = $this->load($item);
        }
        
        return $objects;
        
    }

    /** předáš tomu pole hodnot, podle kterých má hledat. Vrátí jednu entitu */
    function findOneBy(array $values) {
        
    }
    
    /** vrátí všechno */
    function findAll() {
        $items = $this->conn->select('*')->from('menu_item')->fetchAll();
        
        $objects = array();
        
        foreach ($items as $item) {
            $objects[] = $this->load($item);
        }
        
        return $objects;
    }
    
    function findAllMenu() {
        $items = $this->conn->select('*')->from('menu')->fetchAll();
        
        return $items;
    }

    private function setIdentity($item, $id) {
        $item->setId($id);

        return $item;
    }

    public function getAllDataSource() {
        return $this->conn->select('*')->from('menu_item');
    }

}

